package io.gitee.kotle.player.ui.view.exo

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatImageView
import androidx.media3.common.MediaItem
import io.gitee.kotle.player.R
import io.gitee.kotle.player.core.IPlayer
import io.gitee.kotle.player.core.impl.exoplayer.IExoPlayerMode
import io.gitee.kotle.player.ui.invisible
import io.gitee.kotle.player.ui.setInvisible
import io.gitee.kotle.player.ui.visible

/**
 * 屏幕锁
 */
class ExoPlayerLockView : AppCompatImageView, IExoPlayerView {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    /**
     * 当时是锁住状态的图标
     */
    var lockStatusImgRes = R.drawable.icon_player_lock

    /**
     * 当时是解锁状态的图标
     */
    var unLockStatusImgRes = R.drawable.icon_player_unlock

    private var config: ExoPlayerViewConfig<IExoPlayerMode>? = null
    override fun onPlayerViewConfigCreated(
        player: IPlayer<MediaItem, IExoPlayerMode>,
        config: ExoPlayerViewConfig<IExoPlayerMode>
    ) {
        super.onPlayerViewConfigCreated(player, config)
        this.config = config
        config.gestureEnable.observeForever(::onGestureEnable)
        config.motionEvent.observeForever(::onMotionEvent)
        config.singleTapConfirmed.observeForever(::singleTapConfirmed)
        updateImg(config.gestureEnable.value)
        this.setOnClickListener {
            val newStatus = !(config.gestureEnable.value ?: true)
            config.gestureEnable.value = newStatus
            config.singleTapConfirmed.value = newStatus
        }
    }


    override fun onPlayerViewConfigDestroyed(
        player: IPlayer<MediaItem, IExoPlayerMode>,
        config: ExoPlayerViewConfig<IExoPlayerMode>
    ) {
        super.onPlayerViewConfigDestroyed(player, config)
        config.gestureEnable.removeObserver(::onGestureEnable)
        config.motionEvent.removeObserver(::onMotionEvent)
        config.singleTapConfirmed.removeObserver(::singleTapConfirmed)
        this.setOnClickListener(null)
        this.config = null
    }

    private fun onGestureEnable(enable: Boolean?) {
        updateImg(enable)
    }


    private fun updateImg(gestureEnable: Boolean?) {
        if (gestureEnable != false) {
            setImageResource(unLockStatusImgRes)
        } else {
            setImageResource(lockStatusImgRes)
        }
    }

    private fun onMotionEvent(event: MotionEvent?) {
        if (config?.gestureEnable?.value == false && event?.action == MotionEvent.ACTION_DOWN) {
            showAndPostHide()
        }
    }

    private val hideUiRun = Runnable {
        invisible()
    }

    /**
     * 显示图标，并且延时隐藏
     */
    private fun showAndPostHide() {
        visible()
        removeCallbacks(hideUiRun)
        postDelayed(hideUiRun, 3000)
    }


    private fun singleTapConfirmed(b: Boolean?) {
        b ?: return
        removeCallbacks(hideUiRun)
        setInvisible(b)
    }
}